//  
//  Copyright (C) 2009 Alex Allen
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 

using System;
using TwitterLib;
using Gtk;
using System.ComponentModel;

namespace twitalotgtk
{	
	[System.ComponentModel.ToolboxItem(true)]
	public partial class TweetDisplay : Gtk.Bin
	{
		bool _alt = false; //for row striping.
		Tweet _tweet = null;
		
		public TweetDisplay()
		{
			this.Build();
			
			MainClass.Settings.PropertyChanged += HandlePropertyChanged;
		}

		void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			RefreshTweet();
		}
		
		public TweetDisplay(Tweet tweet, bool alt):this()
		{
			AssignTweet(tweet, alt);	
		}
		
		private void ApplySettings()
		{
			if(_alt)
			{
				eventbox1.ModifyBg(StateType.Normal, MainClass.Settings.AltRowColour);
			}
			else
			{
				eventbox1.ModifyBg(StateType.Normal, MainClass.Settings.RowColour);
			}			
		}
			          
		private void RefreshTweet()
		{
			AssignTweet(_tweet, _alt);	
		}
		
		/// <summary>
		/// Assign the given tweet to the control.
		/// </summary>
		/// <param name="tweet"> The tweet to assign.
		/// A <see cref="Tweet"/>
		/// </param>
		/// <param name="colour"> The background colour of the widget
		/// A <see cref="Gdk.Color"/>
		/// </param>
		public void AssignTweet(Tweet tweet, bool alt)		
		{				
			_alt = alt;
			_tweet = tweet;
			lblUser.Text = string.Format("<b>{0}</b> <i>from {1}</i>", _tweet.User.ScreenName, _tweet.Source);
			lblUser.UseMarkup = true;			
		
			if(MainClass.Settings.RelativeTime)
			{
				_tweet.UpdateRelativeTime();	
				lblTime.Text = _tweet.RelativeTime;
			}
			else
			{
				lblTime.Text = _tweet.DateCreated.ToString();
			}
			lblTime.Justify = Gtk.Justification.Right;
			
		 	lblTweet.Text = _tweet.Text;
			//txtTweet.Justification = Gtk.Justification.Left;
			
			ApplySettings();
		}
	}
}
